#include "testingmainwidget.h"
#include "ui_testingmainwidget.h"
#include <QHeaderView>
#include "databaseoperate.h"
#include "qmath.h"
#include <QFileDialog>
#include <QDesktopServices>
testingMainWidget::testingMainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::testingMainWidget)
{
    ui->setupUi(this);
    initForm();
    initData();
    initSuveyorShitfs();
    ui->lineEdit->setValidator(new QIntValidator(0, 1000, this)); //0, 10000为输入的数字值范围
    connect(this, SIGNAL(send2byte(QStringList)), serialPortSet::Instance(), SIGNAL(getList2Byte(QStringList)));
    connect(serialPortSet::Instance(), SIGNAL(readyRead()), this, SLOT(handleSerialReturn()));
    connect(serialPortSet::Instance(), SIGNAL(initFailed()), this, SLOT(setInitFailed()));
    connect(this, SIGNAL(transitHoldTime(int)), serialPortSet::Instance()->bytes,SLOT(setHoldTime(int)));
//    ui->checkBox->hide();//打印数据功能
    ui->pushButton_3->hide();//清理列表功能，暂不开放,合并到开始中
    startAllAction();//初始化
}

testingMainWidget::~testingMainWidget()
{
    delete ui;
}

void testingMainWidget::initForm()
{
    botButton = new bottomLightButton;
    ui->hLayoutLight->addWidget(botButton);
    botButton->setMinimumHeight(100);
    botButton->setMaximumHeight(100);
    rigButton = new rightLightButton;
    ui->vLayoutLight->addWidget(rigButton);
    rigButton->setMaximumWidth(30);

    ui->tableWidget2->setMaximumHeight(200);
    ui->tableWidget2->setColumnWidth(0, 80);
//    ui->tableWidget = new QTableWidget;
    ui->tableWidget->setColumnCount(3); //设置列数
//    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget2->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
//    ui->tableWidget->setRowCount(10);
    ui->tableWidget->verticalHeader()->setVisible(false);
    ui->tableWidget2->horizontalHeader()->setVisible(false);
    QStringList titleList ;
    titleList<<"1"<<"2"<<"3";
    ui->tableWidget->setHorizontalHeaderLabels(titleList);

    headerList<<"序号"<<"开吸阻"<<"闭吸阻";
}
void testingMainWidget::setTableHeader(QStringList strList)
{
    headerList = strList;

}
void testingMainWidget::setTableWidget()
{
    ui->tableWidget->setColumnCount(headerList.count());
    QStringList head = headerList;
    for(int i = 0; i < head.count(); i++)
    {
        if(head.at(i) == "开吸阻")
        {
            head.replace(i, "开吸阻(Pa)");
        }
        if(head.at(i) == "闭吸阻")
        {
            head.replace(i, "闭吸阻(Pa)");
        }
    }
    ui->tableWidget->setHorizontalHeaderLabels(head);
    int width = ui->tableWidget->width() - 77;
    int count = headerList.count();
    ui->tableWidget->setColumnWidth(0, 77);
    for(int i = 1; i < count; i++)
    {
        ui->tableWidget->setColumnWidth(i, width/(count - 1) - 10);
    }
    ui->tableWidget2->setColumnCount(count - 1);
    for(int i = 0; i < count-1; i++)
    {
        ui->tableWidget2->setColumnWidth(i, width/(count - 1) - 10);
    }
    emit send2byte(headerList);
}
//开始测试按钮
void testingMainWidget::on_pushButton_clicked()
{
    if(isStart)
    {
        QMessageBox::warning(NULL, "提示", "已在测量中!");
        return;
    }
    //数据校验
    if(!checkDataFunc())
    {
        return ;
    }
    //清空列表操作
    {
        ifPrint = false;
        haveCheckNum = 0;
        maxOpen = 0;
        maxClose = 0;
        minOpen = 99999;
        minClose = 99999;
        severalOpen = 0.0;
        severalClose = 0.0;
        int Count = ui->tableWidget->rowCount();
        for(int i = 0; i < Count; i++)
        {
            ui->tableWidget->removeRow(0);
        }
        Count = ui->tableWidget2->columnCount();
        for(int i = 0; i < Count; i++)
        {
            ui->tableWidget2->removeColumn(0);
        }
    }
    dateNumber = QDateTime::currentDateTime().toString("yyyyMMddHHmmss");
    ui->statusLabel->setText(dateNumber);
    ui->pushButton_3->setDisabled(true);
    setStartDisabled();
    //测量标识
    isStart = true;
    emit startSignal();
    //灯效
    botButton->startInit();
    rigButton->setStartOrStop(true);
    //
    setTableWidget();
    //下发样品参数指令
    serialPortSet::Instance()->sendSampleInfo2Com();//下发样品参数信息

}
void testingMainWidget::setStartDisabled()
{
    ui->lineEdit->setDisabled(true);
    ui->comboBox->setDisabled(true);
    ui->checkBox->setDisabled(true);
}
void testingMainWidget::startAllAction()
{
    serialPortSet::Instance()->sendInit2Com();//初始化
}
void testingMainWidget::handleSerialReturn()
{
    QByteArray buf;
    buf = serialPortSet::Instance()->serial->readAll();
    qDebug()<<"buf:"<<buf;
    buf = handleCom2ByteReturn(buf);
    QString bufstr = QString(buf);
    if(!bufstr.contains("030d0a"))
    {
        return;
    }
    tmpStr = "";
    QString s = bufstr.mid(4, 4);
    if(s.toInt() == 3830)         //3830为初始化指令的空返回
    {
        if(bufstr.mid(16,2).toInt() == 31)
        {
            qDebug()<<"初始化空返回(成功)";
            tmpStr = "";
            //下发样品参数指令
//            serialPortSet::Instance()->sendSampleInfo2Com();//下发样品参数信息
        }
        else
        {
            qDebug()<<"初始化空返回(失败)";
            QMessageBox::StandardButton result= QMessageBox::information(NULL, "警告", "初始化失败！",QMessageBox::Yes);
            switch (result)
            {
            case QMessageBox::Yes:
                exit(0);
            default:
                exit(0);
                break;
            }
        }
    }
    if(s.toInt() == 3033)       //3033为样品参数信息空返回
    {
        qDebug()<<"样品信息空返回";
        tmpStr = "";
        //下发系统信息参数
        serialPortSet::Instance()->sendSystemInfo2Com();//下发系统参数信息
    }
    if(s.toInt() == 3036)       //3036为系统参数空返回
    {
        qDebug()<<"系统信息空返回";
        tmpStr = "";
        //下发开始测量指令
        serialPortSet::Instance()->sendStart2Com();
    }
    if(s.toInt() == 3031)   //3031为开始测量空返回,测试开始，初始化需要测量的数量
    {
        qDebug()<<"开始测量空返回";
        tmpStr = "";
        needCheckNum = ui->lineEdit->text().toInt();
    }
    if(s.toInt() == 3332)   //测量数据返回
    {
//        QString tData = bufstr.mid(14, 2) ;
        if(bufstr.mid(14, 2).toInt() == 31) //吸阻仪数据
        {
//            QString dataSuc = buf.mid(28, 8);
            QString openData = buf.mid(28, 4);
            QString closeData = buf.mid(32, 4);
            handleOpenCloseData(openData, closeData);
        }
    }
    if(s.toInt() == 3035)
    {
        emit send2Cali(bufstr);
    }
    if(s.toInt() == 3333)
    {
        if(bufstr.mid(14, 2).toInt() == 31)//吸阻
        {
            if(bufstr.mid(16, 2).toInt() == 01) //进入(暂时未开放)
            {

            }
            if(bufstr.mid(16, 2).toInt() == 02) //测试中
            {
                botButton->setLabelInfo(2, "测试中");
                botButton->setLightColorYellow(2);
            }
            if(bufstr.mid(16, 2).toInt() == 03) //排出
            {
                botButton->setLabelInfo(2, "样品排出");
                botButton->setLightColorInit();
            }
        }
    }
    if(s.toInt() == 3331)
    {
        int a = bufstr.mid(16, 2).toInt() ;
        if(bufstr.mid(16, 2).toInt() == 1 )
        {
            QMessageBox::StandardButton result= QMessageBox::information(NULL, "警告", "卡料，清理后点击确认，否则结束测量",QMessageBox::Yes|QMessageBox::No);
            switch (result)
            {
            case QMessageBox::No:
                on_pushButton_2_clicked();//停止按钮槽函数
                return;
            case QMessageBox::Yes:
                serialPortSet::Instance()->sendCleanInfo2Com();
            default:
                break;
            }
        }else if(bufstr.mid(16, 2).toInt() == 2)
        {
            QMessageBox::StandardButton result= QMessageBox::information(NULL, "警告", "缺料，补充后点击确认，否则结束测量",QMessageBox::Yes|QMessageBox::No);
            switch (result)
            {
            case QMessageBox::No:
                on_pushButton_2_clicked();//停止按钮槽函数
                return;
            case QMessageBox::Yes:
                serialPortSet::Instance()->sendAppendInfo2Com();
            default:
                break;
            }
        }
    }
}
void testingMainWidget::handleOpenCloseData(QString openData, QString closeData)
{
    int rowCount = ui->tableWidget->rowCount();
    ui->tableWidget->setRowCount(rowCount+1);
    ui->tableWidget->scrollToBottom();//展示最后一行

    TestInfo tInfo;
    if(headerList.contains("开吸阻") && headerList.contains("闭吸阻"))
    {
        ui->tableWidget->setItem(rowCount, 0, new QTableWidgetItem(QString::number(rowCount+1)));
        ui->tableWidget->setItem(rowCount, 1, new QTableWidgetItem(QString::number(openData.toInt(0, 16))));
        ui->tableWidget->setItem(rowCount, 2, new QTableWidgetItem(QString::number(closeData.toInt(0, 16))));
        //入库
        tInfo.num = QString::number(rowCount+1);
        tInfo.open = QString::number(openData.toInt(0, 16));
        tInfo.close = QString::number(closeData.toInt(0, 16));
        tableInfo.append(tInfo);
        //最大值
        maxOpen = qMax(openData.toInt(0, 16), maxOpen);
        maxClose = qMax(closeData.toInt(0, 16), maxClose);
        ui->tableWidget2->setItem(0, 0, new QTableWidgetItem(QString::number(maxOpen)));
        ui->tableWidget2->setItem(0, 1, new QTableWidgetItem(QString::number(maxClose)));
        //最小值
        minOpen = qMin(openData.toInt(0, 16), minOpen);
        minClose = qMin(closeData.toInt(0, 16), minClose);
        ui->tableWidget2->setItem(1, 0, new QTableWidgetItem(QString::number(minOpen)));
        ui->tableWidget2->setItem(1, 1, new QTableWidgetItem(QString::number(minClose)));
        //平均值
        if(rowCount > 0)
        {
            severalOpen = (severalOpen + openData.toInt(0, 16))/2;
            severalClose = (severalClose + closeData.toInt(0, 16))/2;
        }else if(rowCount == 0)
        {
            severalOpen = openData.toInt(0, 16);
            severalClose = closeData.toInt(0, 16);
        }
        ui->tableWidget2->setItem(2, 0, new QTableWidgetItem(QString::number(severalOpen)));
        ui->tableWidget2->setItem(2, 1, new QTableWidgetItem(QString::number(severalClose)));
    }
    if(!headerList.contains("闭吸阻") && headerList.contains("开吸阻"))
    {
        ui->tableWidget->setItem(rowCount, 0, new QTableWidgetItem(QString::number(rowCount+1)));
        ui->tableWidget->setItem(rowCount, 1, new QTableWidgetItem(QString::number(openData.toInt(0, 16))));
        //入库
        tInfo.num = QString::number(rowCount+1);
        tInfo.open = QString::number(openData.toInt(0, 16));
        tInfo.close = QString::number(closeData.toInt(0, 16));
        tableInfo.append(tInfo);

        maxOpen = qMax(openData.toInt(0, 16), maxOpen);
        ui->tableWidget2->setItem(0, 0, new QTableWidgetItem(QString::number(maxOpen)));
        minOpen = qMin(openData.toInt(0, 16), minOpen);
        ui->tableWidget2->setItem(1, 0, new QTableWidgetItem(QString::number(minOpen)));
        if(rowCount > 0)
        {
            severalOpen = (severalOpen + openData.toInt(0, 16))/2;
        }else if(rowCount == 0)
        {
            severalOpen = openData.toInt(0, 16);
        }
        ui->tableWidget2->setItem(2, 0, new QTableWidgetItem(QString::number(severalOpen)));
    }
    if(headerList.contains("闭吸阻") && !headerList.contains("开吸阻"))
    {
        ui->tableWidget->setItem(rowCount, 0, new QTableWidgetItem(QString::number(rowCount+1)));
        ui->tableWidget->setItem(rowCount, 1, new QTableWidgetItem(QString::number(closeData.toInt(0, 16))));
        //入库
        tInfo.num = QString::number(rowCount+1);
        tInfo.open = QString::number(openData.toInt(0, 16));
        tInfo.close = QString::number(closeData.toInt(0, 16));
        tableInfo.append(tInfo);

        maxClose = qMax(closeData.toInt(0, 16), maxClose);
        ui->tableWidget2->setItem(0, 0, new QTableWidgetItem(QString::number(maxClose)));
        minClose = qMin(closeData.toInt(0, 16), minClose);
        ui->tableWidget2->setItem(1, 0, new QTableWidgetItem(QString::number(minClose)));

        if(rowCount > 0)
        {
            severalClose = (severalClose + closeData.toInt(0, 16))/2;
        }else if(rowCount == 0)
        {
            severalClose = closeData.toInt(0, 16);
        }
        ui->tableWidget2->setItem(2, 0, new QTableWidgetItem(QString::number(severalClose)));
    }

    haveCheckNum = ui->tableWidget->rowCount();
    if(haveCheckNum >= needCheckNum)
    {
//        QMessageBox::warning(NULL, "提示", "测量完毕！");
        on_pushButton_2_clicked();
        //int instId = ui->comboBox_3->currentData(Qt::UserRole).toInt();
        databaseOperate::Instance()->addTestInfo(tableInfo, suveyor, shifts, ui->comboBox->currentText(), ui->comboBox->currentData(Qt::UserRole).toString(), dateNumber);
        tableInfo.clear();
    }

}
void testingMainWidget::calcuStandardDeviation()
{
    qreal Deviation = 0;
    int rowCount = ui->tableWidget->rowCount();
    int colCount = ui->tableWidget->columnCount();
    if(rowCount <= 0)
        return;
    int colCount_2 = ui->tableWidget2->columnCount();

    if(colCount_2 < 1)
        return;
    if(colCount < 2)
        return;
//    if(colCount == 2)
//    {
//        qreal several = ui->tableWidget2->item(2, 0)->text().toDouble();
//        if(several <= 0)
//            return;
//        for(int i = 0; i < rowCount; i++)
//        {
//            Deviation += qPow((ui->tableWidget->item(i, 1)->text().toDouble() - several), 2);
//        }
//        Deviation = qSqrt(Deviation/rowCount);
//        ui->tableWidget2->setItem(3, 0, new QTableWidgetItem(QString::number(Deviation)));
//    }
    if(colCount >= 2)
    {
        for(int i = 1; i < colCount; i++)//循环表1的列数
        {
            Deviation = 0;
            qreal several = ui->tableWidget2->item(2, i-1)->text().toDouble();
            if(several <= 0)
                return;
            for(int j = 0; j < rowCount; j++)
            {
                Deviation += qPow((ui->tableWidget->item(j, i)->text().toDouble() - several), 2);
            }
            Deviation = qSqrt(Deviation/rowCount);
            ui->tableWidget2->setItem(3, i-1, new QTableWidgetItem(QString::number(Deviation)));
        }
    }
    QMessageBox::warning(NULL, "提示", "测量完毕！");
}
QByteArray testingMainWidget::handleCom2ByteReturn(QByteArray byte)
{
    QByteArray nullArray("");
    QString byteStr = tr(byte.toHex());

    tmpStr += byteStr;
    return tmpStr.toLatin1();
}
//停止测试按钮
void testingMainWidget::on_pushButton_2_clicked()
{
    ui->pushButton_3->setDisabled(false);
    rigButton->setStartOrStop(false);
    isStart = false;
    emit stopSignal();
//    serialPortSet::Instance()->sendCleanInfo2Com();//sendStop2Com
    serialPortSet::Instance()->sendStop2Com();
    //停止后可以编辑的控件
    setStopAbled();
    //计算标准偏差
    calcuStandardDeviation();
    //打印数据
    print2Epson();
}
void testingMainWidget::setStopAbled()
{
    ui->checkBox->setDisabled(false);
    ui->lineEdit->setDisabled(false);
    ui->comboBox->setDisabled(false);
}
void testingMainWidget::initData()
{
    QMap<int, QString> sampleList;
    databaseOperate::Instance()->querySample(sampleList);
    QMapIterator<int, QString> i(sampleList);
    while(i.hasNext())
    {
        i.next();
        ui->comboBox->addItem(i.value(), i.key());
    }
}

void testingMainWidget::setSampleComboxChanged(int index)
{
    ui->comboBox->setCurrentIndex(index);
}

bool testingMainWidget::checkDataFunc()
{
    if(ui->lineEdit->text().toInt() <= 0)
    {
        QMessageBox::warning(NULL, "提示", "请正确输入待测试数量！");
        return false;
    }
    if(ui->comboBox->currentText() == "")
    {
        QMessageBox::warning(NULL, "提示", "请选择测试牌号！");
        return false;
    }
    return true;
}
//暂不开放清理功能
void testingMainWidget::on_pushButton_3_clicked()
{

//    haveCheckNum = 0;
//    maxOpen = 0;
//    maxClose = 0;
//    minOpen = 99999;
//    minClose = 99999;
//    severalOpen = 0.0;
//    severalClose = 0.0;
//    int Count = ui->tableWidget->rowCount();
//    for(int i = 0; i < Count; i++)
//    {
//        ui->tableWidget->removeRow(0);
//    }
//    Count = ui->tableWidget2->columnCount();
//    for(int i = 0; i < Count; i++)
//    {
//        ui->tableWidget2->removeColumn(0);
//    }

}
void testingMainWidget::initSuveyorShitfs()
{
    QMap<int, QString> c;
    databaseOperate::Instance()->queryShifts(c);
    QMapIterator<int, QString> i(c);
    while(i.hasNext())
    {
        i.next();
        shifts = i.value();
        break;
    }
    c.clear();
    databaseOperate::Instance()->querySurveyor(c);
    QMapIterator<int, QString> j(c);
    while(j.hasNext())
    {
        j.next();
        suveyor = j.value();
        break;
    }
    return;
}

void testingMainWidget::print2Epson()
{
    if(!ui->checkBox->isChecked())
        return;
    if(ifPrint)
        return;
    QFont font = doc.defaultFont();
    font.setPointSize(font.pointSize());
    doc.setDefaultFont(font);
    QSizeF s = QSizeF(p.logicalDpiX() * (58 / 25.4), p.logicalDpiY() * (297 / 25.4));

    doc.setPageSize(s);

    p.setOutputFormat(QPrinter::NativeFormat);
    htmlStr += "<p style='text-align:center'>吸阻仪 测试结果</p><br>";
    QString currentTime = QTime::currentTime().toString();
    htmlStr += currentTime + "<br>";
    htmlStr += "检验员:" + suveyor + "<br>";
    htmlStr += "班次:" + shifts +"<br>";
    htmlStr += "<p style='text-align:center'>---------------------------------<\p><br>";
    if(headerList.contains("开吸阻") && headerList.contains("闭吸阻"))
    {
        htmlStr += "<tr><th>序号</th><th>开吸阻(Pa)</th><th>闭吸阻(Pa)</th></tr>";
    }
    else if(!headerList.contains("开吸阻") && headerList.contains("闭吸阻"))
    {
        htmlStr += "<tr><th>序号</th><th>闭吸阻(Pa)</th></tr>";
    }
    else if(headerList.contains("开吸阻") && !headerList.contains("闭吸阻"))
    {
        htmlStr += "<tr><th>序号</th><th>开吸阻(Pa)</th></tr>";
    }
//    htmlStr += "<tr><th>序号</th><th>强度(N)</th><th>型变量(mm)</th><th>直径(mm)</th><th>形变率(%)</th><th>弹性指数</tr>";
    font.setPointSize(font.pointSize() - 2);
    doc.setDefaultFont(font);
    for(int i=0; i < ui->tableWidget->rowCount(); i ++)
    {
        htmlStr += "<tr>";
        for(int j = 0; j<ui->tableWidget->columnCount(); j++)
        {

            if(ui->tableWidget->item(i, j) != 0)
            {
              htmlStr += "<th>";
              htmlStr += ui->tableWidget->item(i, j)->text();
              htmlStr += "</th>";
            }else
            {
                htmlStr += "<th>";
                htmlStr += "";
                htmlStr += "</th>";
            }
        }
        htmlStr += "</tr>";
    }
    htmlStr += "<br>";
    htmlStr += "<p style='text-align:center'>---------------------------------<\p><br>";
    for(int i=0; i < ui->tableWidget2->rowCount(); i ++)
    {
        htmlStr += "<tr>";
        for(int j = 0; j <= ui->tableWidget2->columnCount(); j++)
        {
            if(j == 0)
            {
                if(i == 0)
                {
                    htmlStr += "<th>";
                    htmlStr += "最大值";
                    htmlStr += "</th>";
                    continue;
                }
                else if(i == 1)
                {
                    htmlStr += "<th>";
                    htmlStr += "最小值";
                    htmlStr += "</th>";
                    continue;
                }
                else if(i == 2)
                {
                    htmlStr += "<th>";
                    htmlStr += "平均值";
                    htmlStr += "</th>";
                    continue;
                }
                else if(i == 3)
                {
                    htmlStr += "<th>";
                    htmlStr += "标准偏差";
                    htmlStr += "</th>";
                    continue;
                }

            }
            if(ui->tableWidget2->item(i, j-1) != 0)
            {
              htmlStr += "<th>";
              htmlStr += ui->tableWidget2->item(i, j-1)->text();
              htmlStr += "</th>";
            }else
            {
                htmlStr += "<th>";
                htmlStr += "";
                htmlStr += "</th>";
            }
        }
        htmlStr += "</tr>";
    }
    doc.setHtml(htmlStr);
    doc.print(&p);
    ifPrint = true;
    return;
}
void testingMainWidget::saveTabel2Excel()
{
    QString dataTimeStr = QDateTime::currentDateTime().toString("yyyy-MM-dd -hh-mm-ss").remove(QRegExp("\\s"));;
    QString fileName = QFileDialog::getSaveFileName(this, tr("Excel file"), QString("./" +dataTimeStr+".xlsx"), tr("Excel Files(*.xlsx)"));//设置保存的文件名
    if(fileName != "")
    {

        QAxObject *excel = new QAxObject;
        if(excel->setControl("Excel.Application"))
        {
            excel->dynamicCall("SetVisible (bool Visible)", false);
            excel->setProperty("DisplayAlerts", false);

            QAxObject *workbooks = excel->querySubObject("WorkBooks");      //获取工作簿集合
            workbooks->dynamicCall("Add");                                  //新建一个工作簿
            QAxObject *workbook = excel->querySubObject("ActiveWorkBook");  //获取当前工作簿
            QAxObject *worksheet = workbook->querySubObject("Worksheets(int)", 1);
            QAxObject *cell;
            //添加Excel表头数据
            for (int i = 1; i <= ui->tableWidget->columnCount(); i++)
            {
                cell = worksheet->querySubObject("Cells(int, int)", 1, i);
                cell->setProperty("RowHeight", 40);
                cell->setProperty("RowWidth", 150);
                cell->dynamicCall("SetValue(const QString&)", ui->tableWidget->horizontalHeaderItem(i-1)->data(0).toString());
            }
            //将form列表中的数据依次保存到Excel文件中
            for(int i=0;i<ui->tableWidget->rowCount();i++){
                for (int j=0;j<ui->tableWidget->columnCount();j++)
                {
                    cell = worksheet->querySubObject("Cells(int,int)", i+2, j+1);
                    cell->dynamicCall("SetValue(const QString&)", ui->tableWidget->item(i,j)?ui->tableWidget->item(i,j)->text():"");
                }
            }

            int head2 = ui->tableWidget->rowCount() + 3;
//            //添加Excel表头数据
//            for (int i = 1; i <= ui->tableWidget2->columnCount(); i++)
//            {
//                cell = worksheet->querySubObject("Cells(int, int)", head2, i + 1);
//                cell->setProperty("RowHeight", 40);
//                cell->setProperty("RowWidth", 150);
//                cell->dynamicCall("SetValue(const QString&)", ui->tableWidget2->horizontalHeaderItem(i)->data(0).toString());
//            }
            for(int i=0;i<ui->tableWidget2->rowCount();i++)
            {
                for (int j=0;j<ui->tableWidget2->columnCount();j++)
                {
                    cell = worksheet->querySubObject("Cells(int,int)", i+head2+1, j+2);
                    cell->dynamicCall("SetValue(const QString&)", ui->tableWidget2->item(i,j)?ui->tableWidget2->item(i,j)->text():"");
                }
            }
            cell = worksheet->querySubObject("Cells(int,int)", head2+1, 1);
            cell->dynamicCall("SetValue(const QString&)", "最大值");
            cell = worksheet->querySubObject("Cells(int,int)", head2+2, 1);
            cell->dynamicCall("SetValue(const QString&)", "最小值");
            cell = worksheet->querySubObject("Cells(int,int)", head2+3, 1);
            cell->dynamicCall("SetValue(const QString&)", "平均值");
            cell = worksheet->querySubObject("Cells(int,int)", head2+4, 1);
            cell->dynamicCall("SetValue(const QString&)", "标准偏差");
            //将生成的Excel文件保存到指定目录下
            workbook->dynamicCall("SaveAs(const QString&)", QDir::toNativeSeparators(fileName));//保存至fileName
            workbook->dynamicCall("Close()");
            excel->dynamicCall("Quit()");
            delete excel;
            excel = NULL;
            if(QMessageBox::question(NULL, QString::fromUtf8("完成"), QString::fromUtf8("文件已经导出，是否现在打开?"), QMessageBox::Yes|QMessageBox::No) == QMessageBox::Yes)
            {
                QDesktopServices::openUrl(QUrl("file:///" + QDir::toNativeSeparators(fileName)));
            }
        }
    }
}

void testingMainWidget::on_pushButton_4_clicked()
{
    if(ui->tableWidget->rowCount() == 0)
        return;
    QMessageBox::StandardButton result= QMessageBox::information(NULL, "提示", "是否保存数据？",QMessageBox::Yes|QMessageBox::No);
    switch (result)
    {
    case QMessageBox::Yes:
        break;
    case QMessageBox::No:
        return;
    default:
        return;
    }
    saveTabel2Excel();
}
